package com.shihb.chapter07designpattern.proxypattern

import java.lang.reflect.{InvocationHandler, Method}

/**
 * Description:自己调用的代理
 *
 * @author shihb
 * @date 2020/10/4 19:40
 */
class OwnerInvocationHandler extends InvocationHandler {
  var personInfo: PersonInfo = _

  //构造器
  def this(p: PersonInfo) {
    this
    this.personInfo = p
  }

  //这里是proxy就是和OwnerInvocationHandler合作的代理
  @throws(classOf[Throwable])
  override def invoke(proxy: Any, method: Method, args: Array[AnyRef]): AnyRef = {
    method match {
      case fn if fn.getName.startsWith("get") => fn.invoke(personInfo)
      //  自己不能给自己打分
      case fn if "setScore".equals(fn.getName) => new IllegalAccessError()
      case fn if fn.getName.startsWith("set") => fn.invoke(personInfo, args(0).toString())
    }
  }
}


























